package com.hctg.forced2sleep.activity;

import android.content.Intent;
import android.os.Bundle;

import com.actor.myandroidframework.bean.OnActivityCallback;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.hctg.forced2sleep.adapter.AppChooseAdapter;
import com.hctg.forced2sleep.bean.MineItem;
import com.hctg.forced2sleep.databinding.ActivityAppChooseBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 选择应用
 * @author    : ldf
 * date       : 2024/12/30 on 16:16
 */
public class AppChooseActivity extends BaseActivity<ActivityAppChooseBinding> {

    public static final String RESULT_KEY = "item";

    private final AppChooseAdapter mAdapter = new AppChooseAdapter(false);
    private final List<AppUtils.AppInfo> items = new ArrayList<>();
    private final List<AppUtils.AppInfo> itemApps = new ArrayList<>();
    private final List<AppUtils.AppInfo> itemSystems = new ArrayList<>();

    public static void start(BaseActivity<?> activity, OnActivityCallback callback) {
        activity.startActivityForResult(new Intent(activity, AppChooseActivity.class), callback);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showNetWorkLoadingDialog();
        ThreadUtils.executeBySingle(new ThreadUtils.SimpleTask<List<AppUtils.AppInfo>>() {
            @Override
            public List<AppUtils.AppInfo> doInBackground() throws Throwable {
                return AppUtils.getAppsInfo();
            }
            @Override
            public void onSuccess(List<AppUtils.AppInfo> result) {
                dismissNetWorkLoadingDialog();
                items.addAll(result);
                for (AppUtils.AppInfo appInfo : items) {
                    if (appInfo.isSystem()) {
                        itemSystems.add(appInfo);
                    } else {
                        itemApps.add(appInfo);
                    }
                }
                //填充数据
                inflateContent();
            }
            @Override
            public void onFail(Throwable t) {
                super.onFail(t);
                dismissNetWorkLoadingDialog();
            }
        });

        viewBinding.titleBar.ivBack.setOnClickListener(v -> finish());
        viewBinding.titleBar.setTitle("选择应用");
        viewBinding.brgRadioGroup.setOnCheckedChangeListener2((group, checkedId, position, reChecked) -> {
            if (!reChecked) {
                inflateContent();
            }
        });

        //点击item, 返回数据
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            AppUtils.AppInfo item = mAdapter.getData().get(position);
            MineItem mineItem = new MineItem();
            mineItem.name = item.getName();
            mineItem.subName = item.getPackageName();
            setResult(RESULT_OK, new Intent().putExtra(RESULT_KEY, GsonUtils.toJson(mineItem)));
            finish();
        });
        viewBinding.recyclerView.setAdapter(mAdapter);
    }

    /**
     * 填充数据
     */
    private void inflateContent() {
        int checkedPosition = viewBinding.brgRadioGroup.getCheckedPosition();
        if (checkedPosition <= 0) {
            mAdapter.setList(itemApps);
        } else if (checkedPosition == 1) {
            mAdapter.setList(itemSystems);
        } else {
            mAdapter.setList(items);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        items.clear();
        itemApps.clear();
        itemSystems.clear();
    }
}